﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using DBLClient.DBLSearchService;
using DBLClient.DBLUserMemberService;
using DBLClient.DBLUserOrganizationService;
using DBLClient.DBLUserCompanyService;
using DBLClient.DBLUserPersonService;
using DBLClient.DBLUserShopService;
namespace DBLClient.DBLForm
{
    public partial class SearchUserForm : Form
    {
        private DataSet dsSearchResult = new DataSet();
        public SearchUserForm()
        {
            InitializeComponent();
        }

        private void buttonSearchCompany_Click(object sender, EventArgs e)
        {
            //根据公司搜索并显示在列表框中
            SearchService search = new SearchService();
            string searchWord = textBoxSearchCompany.Text;
            if(searchWord.Equals(""))
            {
                return;
            }

            object [] obj = search.SearchCompanyPersonShopOrganzationAll(searchWord, "C", 100);
            dataGridView.DataSource = BriefArrayToDataTable(obj);
        }

        private DataTable BriefArrayToDataTable(object[] source)
        {
            string dblno = string.Empty;
            int id = 0;
            string fullname = string.Empty;
            string infostate = string.Empty;
            DataTable dt = new DataTable();
            dt.Columns.Add("memberid");
            dt.Columns.Add("fullname");
            dt.Columns.Add("dblno");
            dt.Columns.Add("infostate");

            foreach (object o in source)
            {
                BriefInfo b = new BriefInfoComparable();
                b = (BriefInfo)o;
                dblno = b.DBLNo;
                id = b.MemberID;
                fullname = b.FullName;
                infostate = b.InfoState;

                DataRow dr = dt.NewRow();
                dr["memberid"] = id;
                dr["dblno"] = dblno;
                dr["fullname"] = fullname;
                dr["infostate"] = infostate;
                dt.Rows.Add(dr);
            }
            return dt;
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            //新增用户
            AddUserForm addUserForm = new AddUserForm("A","","");
            addUserForm.Show();
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            
            if (dataGridView.CurrentCell != null)
            {
                
                string id = dataGridView.CurrentRow.Cells["ColumnID"].Value.ToString();
                string dblno = dataGridView.CurrentRow.Cells["ColumnDBLNo"].Value.ToString();
                //需要先检查登录用户的地区权限是否有资格编辑这个用户
                if (!Tools.IsMatchAdminLevel(dblno, OnlineUserInfo.LEVEL))
                {
                    MessageBox.Show("你无权修改这个地区的多巴拉用户");
                    return;
                }
                //
                
                AddUserForm updateUser = new AddUserForm("U", id, dblno);
                updateUser.Show();
            }
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            if (dataGridView.CurrentCell == null)
            {
                return;
            }
            if (MessageBox.Show("删除之后不可恢复！", "确定删除此账户吗？", MessageBoxButtons.YesNo)
                == DialogResult.Yes)
            {


                string id =  dataGridView.CurrentRow.Cells["ColumnID"].Value.ToString();
                string dblno = dataGridView.CurrentRow.Cells["ColumnDBLNo"].Value.ToString();
                //需要先检查登录用户的地区权限是否有资格删除这个用户
                if (!Tools.IsMatchAdminLevel(dblno, OnlineUserInfo.LEVEL))
                {
                    MessageBox.Show("你无权删除这个地区的多巴拉用户");
                    return;
                }
                //


                UserMemberService user = new UserMemberService();
                int affect = user.DeleteUserMemberByID(Convert.ToInt32(id));
                if (affect == 1)
                {
                    MessageBox.Show("删除成功");
                }
                else
                {
                    MessageBox.Show("删除不成功");
                }
            }
        }

        private void buttonSearchShop_Click(object sender, EventArgs e)
        {
            //搜索shop并显示在列表框中
            SearchService search = new SearchService();
            string searchWord = textBoxSearchShop.Text;
            if (searchWord.Equals(""))
            {
                return;
            }

            object[] obj = search.SearchCompanyPersonShopOrganzationAll(searchWord, "S", 100);
            dataGridView.DataSource = BriefArrayToDataTable(obj);
        }

        private void buttonSearchPerson_Click(object sender, EventArgs e)
        {
            //搜索shop并显示在列表框中
            SearchService search = new SearchService();
            string searchWord = textBoxSearchPerson.Text;
            if (searchWord.Equals(""))
            {
                return;
            }

            object[] obj = search.SearchCompanyPersonShopOrganzationAll(searchWord, "P", 100);
            dataGridView.DataSource = BriefArrayToDataTable(obj);
        }

        private void buttonSearchOrganization_Click(object sender, EventArgs e)
        {
            //搜索shop并显示在列表框中
            SearchService search = new SearchService();
            string searchWord = textBoxSearchOrganization.Text;
            if (searchWord.Equals(""))
            {
                return;
            }

            object[] obj = search.SearchCompanyPersonShopOrganzationAll(searchWord, "O", 100);
            dataGridView.DataSource = BriefArrayToDataTable(obj);
        }

        private void buttonApproval_Click(object sender, EventArgs e)
        {
            //使之通过审核


            if (dataGridView.CurrentCell == null)
            {
                return;
            }

            int id = Convert.ToInt32(dataGridView.CurrentRow.Cells["ColumnID"].Value);
            string dblno = dataGridView.CurrentRow.Cells["ColumnDBLNo"].Value.ToString();
            //需要先检查登录用户的地区权限是否有资格审核这个用户
            if (!Tools.IsMatchAdminLevel(dblno, OnlineUserInfo.LEVEL))
            {
                MessageBox.Show("你无权审核这个地区的多巴拉用户");
                return;
            }

            UserMemberService user = new UserMemberService();
            DataSet ds = user.GetUserMemberByID(id);
            
            string dbltype = string.Empty;
            if (ds.Tables[0].Rows.Count == 1)
            {
                dbltype = ds.Tables[0].Rows[0]["dbltype"].ToString();
                
            }

            int affectnum = 0;
            switch (dbltype)
            {
                case "O":
                    UserOrganizationService org = new UserOrganizationService();
                    affectnum = org.ApproveUserOrganization(id);
                    break;
                case "P":
                    UserPersonService person = new UserPersonService();
                    affectnum = person.ApproveUserPerson(id);
                    break;
                case "S":
                    UserShopService shop = new UserShopService();
                    affectnum = shop.ApproveUserShop(id);
                    break;
                case "C":
                    UserCompanyService company = new UserCompanyService();
                    affectnum = company.ApproveUserCompany(id);
                    break;
            }

            if (affectnum == 1)
            {
                MessageBox.Show("通过审核成功");
            }
            else
            {
                MessageBox.Show("通过审核失败");
            }
        }

        private void buttonNotApproval_Click(object sender, EventArgs e)
        {
            //使之不通过审核

            if (dataGridView.CurrentCell == null)
            {
                return;
            }

            int id = Convert.ToInt32(dataGridView.CurrentRow.Cells["ColumnID"].Value);
            string dblno = dataGridView.CurrentRow.Cells["ColumnDBLNo"].Value.ToString();
            //需要先检查登录用户的地区权限是否有资格审核这个用户
            if (!Tools.IsMatchAdminLevel(dblno, OnlineUserInfo.LEVEL))
            {
                MessageBox.Show("你无权对这个地区的多巴拉用户审核不通过");
                return;
            }
            UserMemberService user = new UserMemberService();
            DataSet ds = user.GetUserMemberByID(id);

            string dbltype = string.Empty;
            if (ds.Tables[0].Rows.Count == 1)
            {
                dbltype = ds.Tables[0].Rows[0]["dbltype"].ToString();

            }

            int affectnum = 0;
            switch (dbltype)
            {
                case "O":
                    UserOrganizationService org = new UserOrganizationService();
                    affectnum = org.NotApproveUserOrganization(id);
                    break;
                case "P":
                    UserPersonService person = new UserPersonService();
                    affectnum = person.NotApproveUserPerson(id);
                    break;
                case "S":
                    UserShopService shop = new UserShopService();
                    affectnum = shop.NotApproveUserShop(id);
                    break;
                case "C":
                    UserCompanyService company = new UserCompanyService();
                    affectnum = company.NotApproveUserCompany(id);
                    break;
            }

            if (affectnum == 1)
            {
                MessageBox.Show("不通过审核成功");
            }
            else
            {
                MessageBox.Show("不通过审核失败");
            }
        }

        private void buttonSearchLoginEmail_Click(object sender, EventArgs e)
        {
            //搜索shop并显示在列表框中
            UserMemberService search = new UserMemberService();
            string searchWord = textBoxSearchLoginEmail.Text;
            if (searchWord.Equals(""))
            {
                return;
            }


            string dblno = string.Empty;
            
            string fullname = string.Empty;
            string infostate = string.Empty;
            DataTable dt = new DataTable();
            dt.Columns.Add("memberid");
            dt.Columns.Add("fullname");
            dt.Columns.Add("dblno");
            dt.Columns.Add("infostate");
            
            DataSet ds = search.GetUserMemberByLoginEmail(searchWord);
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    DataRow newDataRow = dt.NewRow();
                    newDataRow["memberid"] = dr["id"];
                    newDataRow["dblno"] = dr["dblno"];
                    newDataRow["fullname"] = dr["fullname"];
                    newDataRow["infostate"] = dr["infostate"];

                    dt.Rows.Add(newDataRow);
                }
            }
            dataGridView.DataSource = dt;
        }
    }
}
